--
-- Author: yawei.zhu
-- Date: 2018-09-28 20:38:07
--
local GameDB = import(".GameDB")

function GameDB:getWildBossTicketInfo(id)
	return self:getCachedValue("wildBossTicket", id)
end

function GameDB:getWorldBossConfigByDay(day)
	return self:getCachedValue("worldBossConfig", function(v)
		return tonumber(v.week) == day
	end)
end

function GameDB:getGoddessCardConfig(id)
	return self:getCachedValue("goddess_card", id)
end

function GameDB:getWorldBossNpcId(day, mapId)
	local bossConfig = self:getWorldBossConfigByDay(day)
	local npcs = string.split(bossConfig.npcPos, ";")
	for _, npcData in ipairs(npcs) do
		local infos = string.split(npcData, "|")
		if tonumber(infos[1]) == mapId then
			return tonumber(infos[2])
		end
	end
end

function GameDB:getWolrdBossEndTime()
    local day = app:getWeekDay()
	local worldBossConfig = self:getWorldBossConfigByDay(day)
	local times = string.split(worldBossConfig.endTime, "|")
	local endTime = TimeUtil.getCurDayTargetTimeStamp(tonumber(times[1]), tonumber(times[2]), 0)
    return endTime
end

--获取世界Boss的宝箱奖励
function GameDB:getWorldBossReward(list)
    local day = app:getWeekDay()
    local bossConfig = self:getWorldBossConfigByDay(day)
    local reward
    local rewardinfo, rewardrank
    if list.index == 1 then
        reward = bossConfig.serverReward
    elseif list.index == 2 then
        reward = bossConfig.firstReward
    elseif list.index == 3 then
        reward = bossConfig.guildReward
    end
    local rewardList = string.split(reward, ";")
    for k, v in ipairs(rewardList) do
        local infos = string.split(v, "|")
        if list.index == 1 then
            if tonumber(infos[1]) <= list.result then
                rewardinfo = tonumber(infos[2])
                rewardrank = k
            end
        else 
            if list.result ~= -1 and tonumber(infos[1]) == list.result then
                rewardinfo = tonumber(infos[2])
                break
            end
        end
    end
    return rewardinfo, rewardrank
end

function GameDB:getWorldBossOpenLevel()
    local day = app:getWeekDay()
	local worldBossConfig = self:getWorldBossConfigByDay(day)
    return tonumber(worldBossConfig.level)
end

-- 获取世界BOSS推荐战力需要增加的值 difficultyRevise字段1|0|0|0，第一个参数为开服天数，第四个参数为需要增加的值
function GameDB:getWorldBossDifficultyRevise()
    local extraNum = 0
    local day = app:getWeekDay()
    local worldBossConfig = self:getWorldBossConfigByDay(day)
    local diffStr = worldBossConfig.difficultyRevise
    if diffStr then
        local openServerDays = app.session:getOpenServerDays() -- 开服第几天
        local ret = string.splitTwice(diffStr, {";", "|"})
        for _, v in ipairs(ret) do
            if openServerDays >= tonumber(v[1]) then
                extraNum = v[4] and tonumber(v[4]) or 0
            end
        end
    end
    return extraNum
end

return GameDB
